import {Component, OnDestroy, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {IndexService} from "../../services/index.service";
import {ExtendHttpService} from "../../../common/extend-http.service";
import {TipService} from "../../services/tip.service";
import {Subscription} from "rxjs";

@Component({
  selector: 'app-change-table-settings',
  templateUrl: './change-table-settings.component.html',
  styleUrls: ['./change-table-settings.component.css']
})
export class ChangeTableSettingsComponent implements OnInit, OnDestroy {

  subscribeData: Subscription;

  // 桌子id
  tid = '';

  // 桌子分类列表
  tableClassList = [];

  // 桌子列表
  tableList = [];

  // 已选桌子id
  selectTableId = '';

  // 餐桌分类名称
  tableClass = '';

  // 已下单的餐桌名称
  tableName = '';

  tipType = 'error';
  isLoading = false;

  submitType = true;

  // 订单id
  orderId = '';

  // 返回 url
  constructor(private activatedRoute: ActivatedRoute,
              private indexService: IndexService,
              private extendHttp: ExtendHttpService,
              private tipService: TipService,
              private router: Router
  ) {
    this.activatedRoute.queryParams.subscribe(param => {
      this.tid = param['tid'];
      this.tableClass = param['tableClass'];
      this.tableName = param['tableName'];
      this.orderId = param['orderId'];
    });
  }

  // 桌子分类切换
  changSelect($event) {
    let id = $event.target.value;
    this.tableList = [];
    this.selectTableId = '';
    this.tableClassList.forEach(v => {
      if (v.id == id) {
        this.tableList = v.tableList;
      }
    });
  }

  // 桌子切换
  changSelectTable($event) {
    this.selectTableId = $event.target.value;
  }

  ngOnInit() {
    cBack =()=> {
      $('#btn-back').trigger('click');
    }
    this.subscribeData = this.indexService.tableListEmit.subscribe(res => {
      this.tableClassList = res;
    });

    if (!this.tableClassList.length) {

      // 获取所有餐桌信息
      this.getTableList();
    }
  }

  getTableList() {
    this.extendHttp.post('/merchantApp/singleprocess/getAlltableList', {}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.tableClassList = data.list;
      } else if (data.code == '-2001') {
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  onSubmit() {
    if (!this.isLoading) {
    if (!this.selectTableId.trim()) {
      this.tipType = 'error';
      this.tipService.change(true, '请选择桌台');
      return;
    }
    this.isLoading = !this.isLoading;
    this.extendHttp.post('/merchantApp/singleprocess/changetable', {tid: this.selectTableId, orderId: this.orderId}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.submitType = false;
        this.isLoading = !this.isLoading;
        this.tipType = 'success';
        this.tipService.change(true, '换台成功');

        setTimeout(()=>{
          this.router.navigate(['/main']);
        }, 1500);
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(()=> {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
        this.isLoading = !this.isLoading;
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
      this.isLoading = !this.isLoading;
    });
    }
  }

  ngOnDestroy() {
    this.subscribeData.unsubscribe();
  }

}
